package com.atguigu.controller;


import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.constant.MqConst;
import com.atguigu.entity.*;
import com.atguigu.result.RetVal;
import com.atguigu.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.minidev.json.writer.UpdaterMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 基本销售属性表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/product")
@Api(tags = "sku管理接口")
public class ProductSkuController {
    @Autowired
    private ProductImageService productImageService;
    @Autowired
    private ProductSalePropertyKeyService salePropertyKeyService;
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SearchFeignClient searchFeignClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //4.商品SKU分页列表查询 http://127.0.0.1/product/querySkuInfoByPage/1/10
    @GetMapping("querySkuInfoByPage/{currentPageNum}/{pageSize}")
    @ApiOperation("商品SKU分页列表查询")
    public RetVal querySkuInfoByPage(@PathVariable Long currentPageNum,
                                     @PathVariable Long pageSize) {
        IPage<SkuInfo> page = new Page<>(currentPageNum, pageSize);
        skuInfoService.page(page, null);
        return RetVal.ok(page);
    }
    //http://127.0.0.1/product/queryProductImageByProductId/16
    @GetMapping("queryProductImageByProductId/{spuId}")
    @ApiOperation("根据商品(spuId)查询spu的图片展示")
    public RetVal queryProductImageByProductId(@PathVariable Long spuId) {
        QueryWrapper<ProductImage> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",spuId);
        List<ProductImage> list = productImageService.list(wrapper);
        return RetVal.ok(list);
    }
    //获取销售属性，以及销售属性
    //http://127.0.0.1/product/querySalePropertyByProductId/16
    @GetMapping("querySalePropertyByProductId/{spuId}")
    @ApiOperation("根据商品(spuId)获取销售属性，销售属性值")
    public RetVal querySalePropertyByProductId(@PathVariable Long spuId) {
        List<ProductSalePropertyKey> list = salePropertyKeyService.querySalePropertyByProductId(spuId);
        return RetVal.ok(list);
    }
    //保存 sku
   // http://127.0.0.1/product/saveSkuInfo
    @ApiOperation("保存 sku商品")
    @PostMapping("saveSkuInfo")
    public RetVal saveSkuInfo(@RequestBody SkuInfo SkuInfo) {
       skuInfoService.saveSkuInfo(SkuInfo);
        return RetVal.ok();
    }
    //  spu 下架 0
    // http://127.0.0.1/product/offSale/25
    @GetMapping("offSale/{skuId}")
    @ApiOperation("sku商品下架")
    public RetVal offSale(@PathVariable("skuId") Long skuId) {
        //修改状态1 上驾 0 下架
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setIsSale(0);
        skuInfo.setId(skuId);
        skuInfoService.updateById(skuInfo);
        //TODO 后面涉及到搜索环节的时候再编写
        //searchFeignClient.offSale(skuId);
        //改进 耦合
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.OFF_SALE_ROUTING_KEY,skuId);
        return RetVal.ok();
    }
    // 上架  1
    // http://127.0.0.1/product/onSale/25
    @GetMapping("onSale/{skuId}")
    @ApiOperation("sku商品上架")
    public RetVal onSale(@PathVariable("skuId") Long skuId) {
        //修改状态1 上驾 0 下架
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setIsSale(1);
        skuInfo.setId(skuId);
        skuInfoService.updateById(skuInfo);
        //TODO 后面涉及到搜索环节的时候再编写
        //searchFeignClient.onSale(skuId);
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.ON_SALE_ROUTING_KEY,skuId);
        return RetVal.ok();
    }
}


